﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jxym.DBUtility;
using System.Reflection;
using System.Data.SqlClient;

namespace Jxym.wlkc2011.Module.Default.server.ToStockRoom
{
    public partial class ToStockRoom : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                MsSqlDal<model.StockRoom> iUi = new MsSqlDal<model.StockRoom>();

                object identity = 0;
                string returnStr = "{'success':false,'msg':'参数错误'}";
                int id = 0;
                int tostockroomcount = 0;
                int modelid = 0;

                if (Request.Params["modelid"] != null)
                {

                    modelid = Convert.ToInt32(Request.Params["modelid"]);
                }
                if (Request.Params["ID"] != null)
                {
                    id = Convert.ToInt32(Request.Params["ID"]);
                }
                if (Request.Params["ToStockRoomCount"] != null)
                {
                    tostockroomcount = Convert.ToInt32(Request.Params["ToStockRoomCount"]);
                }
                model.StockRoom sr = new model.StockRoom();
                sr = iUi.GetModel("ID = " + id, null);
                if (sr != null)
                {
                    int result = Convert.ToInt32(sr.StockRoomCount) - tostockroomcount;
                    if (MyGet.GetPermissionsValidation(modelid, "U", Jxym.Common.Request.AdminID("user")))
                    {
                        if (result > 0)
                        {
                            SqlParameter[] values = new SqlParameter[] 
                        {
                            new SqlParameter("@StockRoomCount",result)
                        };

                            iUi.Updata("ID = " + id, values);
                            returnStr = "{'success':true,'msg':'更新完成'}";
                        }
                        else
                        {
                            returnStr = "{'success':true,'msg':'库存不足'}";
                        }
                    }
                    else
                    {
                        returnStr = "{'success':true,'msg':'对不起，您没有权限!'}";
                    }
                }
                else 
                {
                    returnStr = "{'success':true,'msg':'对不起，仓库中不存在此物品!'}";
                }
                Response.Write(returnStr);
            }
        }
    }
}